import Vue from 'vue'
/*
所属分类
*/
Vue.filter('getSort', v => {
	var str
	switch (v) {
		case 'BAGS':
			str = "箱包"
			break
		case 'BEAUTY':
			str = '美妆'
			break
		case 'DIGIT':
			str = '数码'
			break
	}
	return str
}) 

Vue.filter('countDown', (v,fn,getThis,ref,endTime) => {
	
	let that = getThis();
	 let formatDateTime = function(value){
		let hy = value%3600;
		let h = (value-hy)/3600;
		if((h+'').length<2){
		  h = '0'+h;
		}
		let my = hy%60;
		let m = (hy-my)/60;
		if((m+'').length<2){
		  m = '0'+m;
		}
		let s = my;
		if((s+'').length<2){
		  s = '0'+s;
		}
		return h+':'+m+':'+s;
	}
	let remove = function(arr,val) { 
		if(!arr){
			return [];
		}
		var index = arr.indexOf(val); 
		if (index > -1) { 
			arr.splice(index, 1); 
		} 
	}
	let times = 0;
	let url = window.location.href;
	let intervaNumS = sessionStorage.getItem('countDownIntervaNumS');
	if(intervaNumS){
		intervaNumS = JSON.parse(intervaNumS);
	}else{
		intervaNumS = [];
	}
	if(v){
		let nowTime =  ((new Date(v.replace(/-/g,'/')).getTime()+endTime*60*1000)-new Date().getTime())/1000;
		let arr = formatDateTime(nowTime).split(':');
		times = parseInt(arr[0])*60*60+parseInt(arr[1])*60+parseInt(arr[2]);
		if(times==0||times==1){
			fn();
			return '00:00:00';
		}else{
			let intervaNum = setInterval(()=>{
				let intervaNumSnew = sessionStorage.getItem('countDownIntervaNumS');
				if(intervaNumSnew&&intervaNumSnew!='undefined'){
					intervaNumSnew = JSON.parse(intervaNumSnew);
				}else{
					intervaNumSnew = [];
				}
				let newurl = window.location.href;
				times--;
				if(times==0){
					clearInterval(intervaNum);
					if(intervaNumSnew){
						sessionStorage.setItem('countDownIntervaNumS',JSON.stringify(remove(intervaNumSnew,intervaNum)));
					}
					fn();
					return '00:00:00';
				}
				if(newurl!=url){
					intervaNumSnew.forEach(item=>{
						clearInterval(item);
					})
					sessionStorage.removeItem('countDownIntervaNumS')
					return '00:00:00';
				}
				if(that&&that.$refs[ref]){
					if(that.$refs[ref][0]){
						that.$refs[ref][0].innerText = formatDateTime(times);
					}else{
						that.$refs[ref].innerText = formatDateTime(times);
					}
					
				}else{
					clearInterval(intervaNum);
					if(intervaNumSnew){
						sessionStorage.setItem('countDownIntervaNumS',JSON.stringify(remove(intervaNumSnew,intervaNum)));
					}
					
				}
				
			},1000)
			intervaNumS.push(intervaNum);
			sessionStorage.setItem('countDownIntervaNumS',JSON.stringify(intervaNumS));
		}
	}else if(v==0){
		fn();
		return '00:00:00';
	}
	return formatDateTime(times);
})
Vue.filter('mobileSecretFormat', value => {
	if (!value) return '--';

  return value.slice(0,3) + '****' + value.slice(-4);
})

Vue.filter('consigneeFormat', value => {
	if(!value){
		return '';
	}

	if(value.length<5){
		return value;
	}

	return value.substring(0,3)+'...';
})

Vue.filter('amountFormat', (value,flag) => {
	if (!value) return 0;
	value = value*1;
	let result;
	if(flag){
	  value = value.toFixed(0);
	}else{
	  value = value.toFixed(2);
	}
	result = value.replace(/\B(?=(\d{3})+\b)/g,',');
	return result
})